package com.campus.backend.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.backend.bean.feedback.FeedbackTypeBean;
import com.campus.backend.bean.feedback.FeedbackTypeQueryParam;
import com.campus.backend.entity.FeedbackType;
import com.campus.backend.mapper.FeedbackTypeMapper;
import com.campus.backend.vo.feedback.FeedbackTypeVo;
import com.campus.common.bean.common.PageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 反馈类型配置表 服务实现类
 * </p>
 *
 * @author ZhaoYuJie
 * @since 2025-06-06
 */
@Service
public class FeedbackTypeService extends ServiceImpl<FeedbackTypeMapper, FeedbackType> {

    /**
     * 分页查询反馈类型列表
     *
     * @param param 查询参数
     * @return 分页结果
     */
    public PageInfo<FeedbackTypeVo> getFeedbackTypeListPage(FeedbackTypeQueryParam param) {
        PageInfo<FeedbackType> pageInfo = PageHelper.startPage(param.getPageNo(), param.getPageSize())
                .doSelectPageInfo(() -> this.getFeedbackTypeDataList(param));

        // 转换为VO
        List<FeedbackTypeVo> voList = pageInfo.getList().stream()
                .map(feedbackType -> {
                    FeedbackTypeVo vo = new FeedbackTypeVo();
                    BeanUtil.copyProperties(feedbackType, vo);
                    
                    // 设置用户类型名称
                    if (feedbackType.getUserType() != null) {
                        switch (feedbackType.getUserType()) {
                            case 1:
                                vo.setUserTypeName("顾客");
                                break;
                            case 2:
                                vo.setUserTypeName("商户");
                                break;
                            case 3:
                                vo.setUserTypeName("配送员");
                                break;
                            default:
                                vo.setUserTypeName("未知");
                                break;
                        }
                    }
                    
                    // 设置状态名称
                    if (feedbackType.getStatus() != null) {
                        vo.setStatusName(feedbackType.getStatus()==1 ? "启用" : "禁用");
                    }
                    
                    return vo;
                }).collect(Collectors.toList());

        return PageUtil.convertPageInfo(pageInfo, voList);
    }

    private List<FeedbackType> getFeedbackTypeDataList(FeedbackTypeQueryParam param) {
        LambdaQueryWrapper<FeedbackType> queryWrapper = new LambdaQueryWrapper<>();
        
        // 反馈类型名称模糊查询
        if (StrUtil.isNotBlank(param.getName())) {
            queryWrapper.like(FeedbackType::getName, param.getName());
        }
        
        // 反馈类型编码
        if (StrUtil.isNotBlank(param.getCode())) {
            queryWrapper.eq(FeedbackType::getCode, param.getCode());
        }
        
        // 适用用户类型
        if (param.getUserType() != null) {
            queryWrapper.eq(FeedbackType::getUserType, param.getUserType());
        }
        
        // 状态
        if (param.getStatus() != null) {
            queryWrapper.eq(FeedbackType::getStatus, param.getStatus());
        }
        
        // 按排序序号升序，创建时间倒序
        queryWrapper.orderByAsc(FeedbackType::getSortOrder)
                   .orderByDesc(FeedbackType::getCreateTime);
        
        return this.list(queryWrapper);
    }
    
    /**
     * 新增反馈类型
     *
     * @param param 新增参数
     * @return 反馈类型ID
     */
    public Integer addFeedbackType(FeedbackTypeBean param) {
        // 检查编码是否重复
        LambdaQueryWrapper<FeedbackType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FeedbackType::getCode, param.getCode());
        if (this.count(queryWrapper) > 0) {
            throw new RuntimeException("反馈类型编码已存在");
        }
        
        FeedbackType feedbackType = new FeedbackType();
        BeanUtil.copyProperties(param, feedbackType);
        
        // 设置默认值
        if (feedbackType.getStatus() == null) {
            feedbackType.setStatus(1); // 默认启用
        }
        if (feedbackType.getSortOrder() == null) {
            feedbackType.setSortOrder(0); // 默认排序
        }
        
        this.save(feedbackType);
        return feedbackType.getId();
    }
    
    /**
     * 更新反馈类型
     *
     * @param param 更新参数
     */
    public void updateFeedbackType(FeedbackTypeBean param) {
        FeedbackType feedbackType = this.getById(param.getId());
        if (feedbackType == null) {
            throw new RuntimeException("反馈类型不存在");
        }
        
        // 检查编码是否重复（排除自己）
        LambdaQueryWrapper<FeedbackType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FeedbackType::getCode, param.getCode())
                   .ne(FeedbackType::getId, param.getId());
        if (this.count(queryWrapper) > 0) {
            throw new RuntimeException("反馈类型编码已存在");
        }
        
        BeanUtil.copyProperties(param, feedbackType, "id");
        this.updateById(feedbackType);
    }
    
    /**
     * 删除反馈类型
     *
     * @param id 反馈类型ID
     */
    public void deleteFeedbackType(Integer id) {
        FeedbackType feedbackType = this.getById(id);
        if (feedbackType == null) {
            throw new RuntimeException("反馈类型不存在");
        }
        
        this.removeById(id);
    }
    
    /**
     * 根据ID获取反馈类型详情
     *
     * @param typeId 反馈类型ID
     * @return 反馈类型详情
     */
    public FeedbackTypeVo getFeedbackTypeById(Integer typeId) {
        FeedbackType feedbackType = this.getById(typeId);
        if (feedbackType == null) {
            throw new RuntimeException("反馈类型不存在");
        }
        
        FeedbackTypeVo vo = new FeedbackTypeVo();
        BeanUtil.copyProperties(feedbackType, vo);
        
        // 设置用户类型名称
        if (feedbackType.getUserType() != null) {
            switch (feedbackType.getUserType()) {
                case 1:
                    vo.setUserTypeName("顾客");
                    break;
                case 2:
                    vo.setUserTypeName("商户");
                    break;
                case 3:
                    vo.setUserTypeName("配送员");
                    break;
                default:
                    vo.setUserTypeName("未知");
                    break;
            }
        }
        
        // 设置状态名称
        if (feedbackType.getStatus() != null) {
            vo.setStatusName(feedbackType.getStatus()==1 ? "启用" : "禁用");
        }
        
        return vo;
    }
    
    /**
     * 获取所有启用的反馈类型（用于下拉选择）
     *
     * @return 反馈类型列表
     */
    public List<FeedbackTypeVo> getAllEnabledFeedbackTypes() {
        LambdaQueryWrapper<FeedbackType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FeedbackType::getStatus, 1)
                   .orderByAsc(FeedbackType::getSortOrder)
                   .orderByDesc(FeedbackType::getCreateTime);
        
        List<FeedbackType> feedbackTypeList = this.list(queryWrapper);
        
        return feedbackTypeList.stream().map(feedbackType -> {
            FeedbackTypeVo vo = new FeedbackTypeVo();
            BeanUtil.copyProperties(feedbackType, vo);
            
            // 设置用户类型名称
            if (feedbackType.getUserType() != null) {
                switch (feedbackType.getUserType()) {
                    case 1:
                        vo.setUserTypeName("顾客");
                        break;
                    case 2:
                        vo.setUserTypeName("商户");
                        break;
                    case 3:
                        vo.setUserTypeName("配送员");
                        break;
                    default:
                        vo.setUserTypeName("未知");
                        break;
                }
            }
            
            // 设置状态名称
            if (feedbackType.getStatus() != null) {
                vo.setStatusName(feedbackType.getStatus()==1 ? "启用" : "禁用");
            }
            
            return vo;
        }).collect(Collectors.toList());
    }
}
